package com.salon.modules.app.controller;

import com.salon.common.http.response.ObjectResult;
import com.salon.common.http.response.PageResult;
import com.salon.common.http.response.ResultCode;
import com.salon.modules.app.annotation.Login;
import com.salon.modules.makeup.entity.BannerEntity;
import com.salon.modules.makeup.entity.CustomEntity;
import com.salon.modules.makeup.entity.ReservationEntity;
import com.salon.modules.makeup.entity.ServiceEntity;
import com.salon.modules.makeup.service.BannerService;
import com.salon.modules.makeup.service.CustomService;
import com.salon.modules.makeup.service.ReservationService;
import com.salon.modules.makeup.service.ServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * H5接口
 */
@RestController
@RequestMapping("/app")
@Api(tags = "H5接口")
public class MakeUpController {

    @Resource
    private BannerService bannerService;

    @Resource
    private CustomService customService;

    @Resource
    private ServiceService serviceService;

    @Resource
    private ReservationService reservationService;

    @GetMapping("/banner/list")
    @ApiOperation("横幅查询")
    public ObjectResult<List<BannerEntity>> bannerList(Integer position) {
        Map<String, Object> param = new HashMap<>();
        param.put("position", position);
        List<BannerEntity> list = bannerService.listBanner(param);
        return ObjectResult.success(list);
    }

    @GetMapping("/custom/page")
    @ApiOperation("横幅查询")
    public PageResult<CustomEntity> customListPage(@ApiIgnore @RequestParam Map<String, Object> params) {
        return customService.customListPage(params);
    }

    @GetMapping("/service/page")
    @ApiOperation("服务查询")
    public PageResult<ServiceEntity> serviceListPage(@ApiIgnore @RequestParam Map<String, Object> params) {
        return serviceService.serviceListPage(params);
    }

    @GetMapping("/service/info")
    @ApiOperation("服务详情")
    public ObjectResult<ServiceEntity> getServiceInfo(Integer serviceId) {
        ServiceEntity info = serviceService.getById(serviceId);
        return ObjectResult.success(info);
    }

    @Login
    @PostMapping("/service/saveReservation")
    @ApiOperation("预约服务")
    public ObjectResult<String> saveReservation(ReservationEntity entity, @RequestAttribute("userId") Long userId) {
        if (ObjectUtils.isEmpty(userId)) {
            return ObjectResult.failed(ResultCode.FAIL, "参数错误");
        }
        entity.setUserId(userId);
        reservationService.save(entity);
        return ObjectResult.success();
    }

    @Login
    @GetMapping("/reservation/my")
    @ApiOperation("预约服务列表")
    public PageResult<ReservationEntity> myReservation(
            @ApiIgnore @RequestParam Map<String, Object> params,
            @RequestAttribute("userId") Long userId) {
        params.put("userId", userId);
        return reservationService.listMyReservationPage(params);
    }

    @Login
    @GetMapping("/reservation/myDetail")
    @ApiOperation("预约服务详情")
    public ObjectResult<ReservationEntity> getMyReservationDetail(Integer id) {
        return ObjectResult.success(reservationService.getReservationById(id));
    }

    @Login
    @GetMapping("/reservation/finish")
    @ApiOperation("完成预约")
    public ObjectResult<String> finishReservation(Long id) {
        ReservationEntity entity = new ReservationEntity();
        entity.setId(id);
        entity.setStatus(1);
        reservationService.updateById(entity);
        return ObjectResult.success();
    }

    @Login
    @GetMapping("/reservation/cancel")
    @ApiOperation("取消预约")
    public ObjectResult<String> cancelReservation(Long id) {
        ReservationEntity entity = new ReservationEntity();
        entity.setId(id);
        entity.setStatus(2);
        reservationService.updateById(entity);
        return ObjectResult.success();
    }

}
